package com.github.ilioili.justdoit.components.activity;

import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AutoCompleteTextView;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.AppBaseActivity;
import com.github.ilioili.justdoit.common.config.AnimationConfig;
import com.github.ilioili.justdoit.common.config.Colors;
import com.github.ilioili.justdoit.model.dao.CategoryDao;
import com.github.ilioili.justdoit.model.pojo.Category;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

@Layout(R.layout.activity_create_or_modify_category)
public class CreateOrModifyCategoryActivity extends AppBaseActivity {
    @Id(R.id.et_name)
    private AutoCompleteTextView etName;
    @Id(R.id.toolbar)
    private Toolbar toolbar;
    @Id(R.id.coordinatorLayout)
    private CoordinatorLayout vTopLayout;

    private Category category;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (null == category) {
            category = new Category();
            category.color = Colors.getRandomColor();
        }
        vTopLayout.setBackgroundColor(category.color);
        etName.setText(category.name);
    }

    public void onClickDone(View v) {
        String text = etName.getText().toString().trim();
        if (TextUtils.isEmpty(text)) {
            Snackbar.make(etName, R.string.snack_what_do_u_wanna_do, Snackbar.LENGTH_SHORT).show();
            etName.postDelayed(new Runnable() {
                @Override
                public void run() {
                    etName.startAnimation(AnimationUtils.loadAnimation(CreateOrModifyCategoryActivity.this, R.anim.shake));
                    etName.requestFocus();
                }
            }, Snackbar.LENGTH_SHORT);
            return;
        }
        category.name = text;
        if (CategoryDao.insert(category)) {
            setResult(Activity.RESULT_OK);
            finish();
        } else {
            Snackbar.make(etName, R.string.category_already_exit, Snackbar.LENGTH_SHORT).show();
        }
    }

    public static Intent newIntent(Context context) {
        Intent intent = new Intent(context, CreateOrModifyCategoryActivity.class);
        return intent;
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    //选择主题颜色
    public void onClickTheme(View v) {
        startActivity(PickColorActivity.newIntent(this, new PickColorActivity.OnColorSelectListener() {
            @Override
            public void onSelect(int color) {
                final int colorStart = category.color;
                final int endColor = color;
                category.color = color;
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1).setDuration(AnimationConfig.LONG);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    ArgbEvaluator argbEvaluator = new ArgbEvaluator();

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float animatedValue = (float) animation.getAnimatedValue();
                        int color = (int) argbEvaluator.evaluate(animatedValue, colorStart, endColor);
                        vTopLayout.setBackgroundColor(color);
                    }
                });
                valueAnimator.start();
            }
        }));
    }
}
